package com.ming_za.od_updater.core.updater.handler;

import com.ming_za.od_updater.core.updater.StateBridge;
import com.ming_za.od_updater_sdk.IUpdater;
import com.ming_za.od_updater_sdk.transmit.BasisStreamTransmit;

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicLong;

public class ModTransmitHandle extends BasisStreamTransmit {

    private long lastRead = 0;

    private final AtomicLong size = new AtomicLong(0);

    @Override
    public void transmit(OutputStream outputStream, byte[] buff, int buffSize, IUpdater odUpdater) throws IOException {
        super.transmit(outputStream, buff, buffSize, odUpdater);

        size.addAndGet(buffSize);

        if (System.currentTimeMillis() - lastRead > 1000) {
            lastRead = System.currentTimeMillis();
            StateBridge.setSpeed((int) (size.get() / 1024));
            size.set(0);
        }
    }
}
